package de.bloxel.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;

/**
 * @since 0.1.0
 * @author Klaus Hauschild
 */
public class BloxelApplicationSettingsProvider {

  private static final Logger LOG = LoggerFactory.getLogger(BloxelApplicationSettingsProvider.class);

  private static final File settingsFile = new File("bloxel.settings");

  private final AppSettings defaultSettings;

  public BloxelApplicationSettingsProvider(final AppSettings theDefaultSettings) {
    defaultSettings = theDefaultSettings;
  }

  public AppSettings getAppSettings() {
    if (!settingsFile.exists()) {
      return fromDialogAndSave();
    } else {
      return fromFile();
    }
  }

  private AppSettings fromDialogAndSave() {
    if (!JmeSystem.showSettingsDialog(defaultSettings, false)) {
      throw new IllegalStateException("Settings dialog aborted.");
    }
    // this is necessary to force LWJGL
    defaultSettings.setRenderer(AppSettings.LWJGL_OPENGL2);
    FileOutputStream settingOutputStream = null;
    try {
      settingsFile.createNewFile();
      settingOutputStream = FileUtils.openOutputStream(settingsFile);
      defaultSettings.save(settingOutputStream);
      return defaultSettings;
    } catch (final IOException e) {
      LOG.error("Unable to write settings file '{}'", settingsFile, e);
      return null;
    } finally {
      IOUtils.closeQuietly(settingOutputStream);
    }
  }

  private AppSettings fromFile() {
    FileInputStream settingsInputStream = null;
    try {
      settingsInputStream = FileUtils.openInputStream(settingsFile);
      final AppSettings loadedAppSettings = new AppSettings(false);
      loadedAppSettings.load(settingsInputStream);
      return loadedAppSettings;
    } catch (final IOException e) {
      LOG.error("Unable to read settings file '{}'", settingsFile, e);
      return null;
    } finally {
      IOUtils.closeQuietly(settingsInputStream);
    }
  }

}
